package cn.appsys.service.developer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import cn.appsys.dao.appinfo.AppInfoMapper;
import cn.appsys.dao.appversion.AppVersionMapper;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;

@Service
public class AppInfoServiceImpl implements AppInfoService {

	@Resource
	private AppInfoMapper mapper;
	
	@Resource
	AppVersionMapper appVersionMapper;

	@Override
	public List<AppInfo> getAppInfo(
			String softwareName, 
			Integer status, 
			Integer flatformId, 
			Integer categoryLevel1,
			Integer categoryLevel2, 
			Integer categoryLevel3,
			int devId,
			int currentPageNo, 
			int pageSize) {
		List<AppInfo> applist = new ArrayList<AppInfo>();
		try {
			applist = mapper.getAppInfo(
					softwareName, status, 
					flatformId, categoryLevel1, 
					categoryLevel2,
					categoryLevel3,
					devId,
					currentPageNo,
					pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return applist;
	}

	@Override
	public int getCount(
			String softwareName, 
			Integer status, 
			Integer flatformId,
			Integer categoryLevel1,
			Integer categoryLevel2,
			Integer categoryLevel3, 
			int devId) {
		return mapper.getCount(
				softwareName, 
				status, flatformId, 
				categoryLevel1, 
				categoryLevel2, 
				categoryLevel3, 
				devId);
	}

	/*
	 * 新增APP信息
	 */
	@Override
	public boolean add(AppInfo appInfo) throws Exception {
		boolean flag = false;
		if (mapper.add(appInfo) > 0) {
			flag = true;
		}
		return flag;
	}

	/*
	 * 验证APPName是否唯一 (non-Javadoc)
	 * 
	 * @see
	 * cn.appsys.service.developer.AppInfoService#getAppName(java.lang.String)
	 */
	@Override
	public AppInfo getAppsoftwareName(String softwareName) throws Exception {
		return mapper.getAppsoftwareName(softwareName);
	}

	/*
	 * 验证APPName是否唯一 (non-Javadoc)
	 * 
	 * @see
	 * cn.appsys.service.developer.AppInfoService#getAppName(java.lang.String)
	 */
	@Override
	public AppInfo getAppName(String APKName) throws Exception {
		return mapper.getAppName(APKName);
	}

	@Override
	public AppInfo getAppInfoS(Integer id, String APKName) throws Exception {
		return mapper.getAppInfoS(id,APKName);
	}

	@Override
	public AppInfo getAppById(int id) throws Exception {
		return mapper.getAppById(id);
	}

	@Override
	public int modify(AppInfo appInfo) throws Exception {
		return mapper.modify(appInfo);
	}

	@Override
	public int deleteAppLogo(Integer id) throws Exception {
		return mapper.deleteAppLogo(id);
	}

	@Override
	public boolean appsysUpdateSaleStatusByAppId(AppInfo appInfoObj) throws Exception {
		/*
		 * 上架： 
			1 更改status由【2 or 5】 to 4 ， 上架时间
			2 根据versionid 更新 publishStauts 为 2 
			
			下架：
			更改status 由4给为5
		 */
		
		Integer operator = appInfoObj.getModifyBy();
		if(operator < 0 || appInfoObj.getId() < 0 ){
			throw new Exception();
		}
		
		//get appinfo by appid
		AppInfo appInfo = mapper.getAppInfoS(appInfoObj.getId(), null);
		if(null == appInfo){
			return false;
		}else{
			switch (appInfo.getStatus()) {
				case 2: //当状态为审核通过时，可以进行上架操作
					onSale(appInfo,operator,4,2);
					break;
				case 5://当状态为下架时，可以进行上架操作
					onSale(appInfo,operator,4,2);
					break;
				case 4://当状态为上架时，可以进行下架操作
					offSale(appInfo,operator,5);
					break;

			default:
				return false;
			}
		}
		return true;
	}
	private void onSale(AppInfo appInfo,Integer operator,Integer appInfStatus,Integer versionStatus) throws Exception{
		offSale(appInfo,operator,appInfStatus);
		setSaleSwitchToAppVersion(appInfo,operator,versionStatus);
	}
	private boolean offSale(AppInfo appInfo,Integer operator,Integer appInfStatus) throws Exception{
		AppInfo _appInfo = new AppInfo();
		_appInfo.setId(appInfo.getId());
		_appInfo.setStatus(appInfStatus);
		_appInfo.setModifyBy(operator);
		_appInfo.setOffSaleDate(new Date(System.currentTimeMillis()));
		mapper.modify(_appInfo);
		return true;
	}
	private boolean setSaleSwitchToAppVersion(AppInfo appInfo,Integer operator,Integer saleStatus) throws Exception{
		AppVersion appVersion = new AppVersion();
		appVersion.setId(appInfo.getVersionId());
		appVersion.setPublishStatus(saleStatus);
		appVersion.setModifyBy(operator);
		appVersion.setModifyDate(new Date(System.currentTimeMillis()));
		appVersionMapper.modify(appVersion);
		return false;
		
	}

}
